use "dta/import1909.dta" , clear
keep knr valgkrets year *votes*r1* *votes*r2*
order year knr valgkrets *votes*r1* *votes*r2*

foreach round in 1 2 {
gen votes`round'_s=svotesr`round'c1+ svotesr`round'c2+ svotesr`round'c3
gen votes`round'_a=avotesr`round'c1+ avotesr`round'c2
gen votes`round'_v_a=v_avotesr`round'c1+ v_avotesr`round'c2
gen votes`round'_v=vvotesr`round'c1+ vvotesr`round'c2+ vvotesr`round'c3+ vvotesr`round'c4+ vvotesr`round'c5
gen votes`round'_l=lvotesr`round'c1+ lvotesr`round'c2
gen votes`round'_t=tvotesr`round'c1+ tvotesr`round'c2
gen votes`round'_kir=kirvotesr`round'c1+ kirvotesr`round'c2
gen votes`round'_fv=fvvotesr`round'c1+ fvvotesr`round'c2
gen votes`round'_h_fv=h_fvvotesr`round'c1+ h_fvvotesr`round'c2
gen votes`round'_h=hvotesr`round'c1+ hvotesr`round'c2
gen votes`round'_spr=sprvotesr`round'c1+ sprvotesr`round'c2
gen votes`round'_oth=othvotesr`round'c1+ othvotesr`round'c2
}

egen test=rowtotal(svotesr1c1- othvotesr1c2)
egen test2=rowtotal(votes1_s-votes1_oth)
assert test==test2
egen test3=rowtotal(svotesr2c1- svotesr2c3) /* ordering is strange here */
egen test4=rowtotal(votes2_s-votes2_oth)
assert test3==test4

keep year knr valgkrets votes1_* votes2_*
sort year knr valgkrets
save dta/ImportVotes1909.dta, replace


use "dta/import1912.dta" , clear
keep knr valgkrets year *votes*r1* *votes*r2*
order year knr valgkrets *votes*r1* *votes*r2*

foreach round in 1 2 {
gen votes`round'_s=svotesr`round'c1+ svotesr`round'c2
gen votes`round'_a=avotesr`round'c1+ avotesr`round'c2
gen votes`round'_v_a=v_avotesr`round'c1+ v_avotesr`round'c2
gen votes`round'_v=vvotesr`round'c1+ vvotesr`round'c2+ vvotesr`round'c3+ vvotesr`round'c4
gen votes`round'_l=lvotesr`round'c1+ lvotesr`round'c2
gen votes`round'_t=tvotesr`round'c1+ tvotesr`round'c2
gen votes`round'_kir=kirvotesr`round'c1+ kirvotesr`round'c2
gen votes`round'_fv=fvvotesr`round'c1+ fvvotesr`round'c2
gen votes`round'_h_fv=h_fvvotesr`round'c1+ h_fvvotesr`round'c2
gen votes`round'_h=hvotesr`round'c1+ hvotesr`round'c2
gen votes`round'_spr=sprvotesr`round'c1+ sprvotesr`round'c2
gen votes`round'_oth=othvotesr`round'c1+ othvotesr`round'c2
}
egen test=rowtotal(svotesr1c1- othvotesr1c2)
egen test2=rowtotal(votes1_s-votes1_oth)
assert test==test2
egen test3=rowtotal(svotesr2c1- vvotesr2c4)
egen test4=rowtotal(votes2_s-votes2_oth)

keep year knr valgkrets votes1_* votes2_*
sort year knr valgkrets
save dta/ImportVotes1912.dta, replace


use "dta/import1915.dta" , clear
keep knr valgkrets year *votes*r1* *votes*r2*
order year knr valgkrets *votes*r1* *votes*r2*

foreach round in 1 2 {
gen votes`round'_s=svotesr`round'c1+ svotesr`round'c2
gen votes`round'_a=avotesr`round'c1+ avotesr`round'c2
gen votes`round'_v_a=v_avotesr`round'c1+ v_avotesr`round'c2
gen votes`round'_v=vvotesr`round'c1+ vvotesr`round'c2+ vvotesr`round'c3
gen votes`round'_l=lvotesr`round'c1+ lvotesr`round'c2
gen votes`round'_t=tvotesr`round'c1+ tvotesr`round'c2
gen votes`round'_kir=kirvotesr`round'c1+ kirvotesr`round'c2
gen votes`round'_fv=fvvotesr`round'c1+ fvvotesr`round'c2
gen votes`round'_h_fv=h_fvvotesr`round'c1+ h_fvvotesr`round'c2
gen votes`round'_h=hvotesr`round'c1+ hvotesr`round'c2
gen votes`round'_spr=sprvotesr`round'c1+ sprvotesr`round'c2
gen votes`round'_oth=othvotesr`round'c1+ othvotesr`round'c2
}

egen test=rowtotal(svotesr1c1- othvotesr1c2)
egen test2=rowtotal(votes1_s-votes1_oth)
assert test==test2
egen test3=rowtotal(svotesr2c1- vvotesr2c3)
egen test4=rowtotal(votes2_s-votes2_oth)
assert test3==test4

keep year knr valgkrets votes1_* votes2_*
sort year knr valgkrets
save dta/ImportVotes1915.dta, replace


use "dta/import1918.dta" , clear
keep knr valgkrets year *votes*r1* *votes*r2*
order year knr valgkrets *votes*r1* *votes*r2*

foreach round in 1 2 {
gen votes`round'_s=svotesr`round'c1+ svotesr`round'c2
gen votes`round'_a=avotesr`round'c1+ avotesr`round'c2
gen votes`round'_v_a=v_avotesr`round'c1+ v_avotesr`round'c2
gen votes`round'_v=vvotesr`round'c1+ vvotesr`round'c2+ vvotesr`round'c3 +vvotesr`round'c4
gen votes`round'_l=lvotesr`round'c1+ lvotesr`round'c2
gen votes`round'_t=tvotesr`round'c1+ tvotesr`round'c2
gen votes`round'_kir=kirvotesr`round'c1+ kirvotesr`round'c2
gen votes`round'_fv=fvvotesr`round'c1+ fvvotesr`round'c2
gen votes`round'_h_fv=h_fvvotesr`round'c1+ h_fvvotesr`round'c2
gen votes`round'_h=hvotesr`round'c1+ hvotesr`round'c2
gen votes`round'_spr=sprvotesr`round'c1+ sprvotesr`round'c2
gen votes`round'_oth=othvotesr`round'c1+ othvotesr`round'c2
}

egen test=rowtotal(svotesr1c1- othvotesr1c2)
egen test2=rowtotal(votes1_s-votes1_oth)
assert test==test2
egen test3=rowtotal(svotesr2c1- vvotesr2c3)
egen test4=rowtotal(votes2_s-votes2_oth)
assert test3==test4

keep year knr valgkrets votes1_* votes2_*
sort year knr valgkrets
save dta/ImportVotes1918.dta, replace

***************************************
***************************************
***************************************

use dta/ImportVotes1909.dta, clear
append using dta/ImportVotes1912.dta
append using dta/ImportVotes1915.dta
append using dta/ImportVotes1918.dta

drop *spr* /* DROPPING ALL VOTES IN RESIDUAL CATEGORY "SPREDTE", on average 2.5half votes per municipality in first round, 1 vote in second round */


************************************************************************
************************************************************************
***** rename SMD valgkrets
append using dta/Storting1921_municipality
append using dta/Storting1924_municipality
append using dta/Storting1927_municipality

*************************************************************************
******** LET ALL POST-REFORM VOTES BE MEASURED AS "FIRST-ROUND"**********
*************************************************************************
foreach var of varlist votes1_s- votes1_oth {
replace `var'=0 if year>1920
}

foreach var of varlist votes2_s- votes2_oth {
replace `var'=0 if year>1920
}

gen votes1_nsa=0  /*party entering in 1921 */
gen votes1_nkp=0  /*party entering in 1924 */
gen votes2_nsa=0  /*party entering in 1921 */
gen votes2_nkp=0  /*party entering in 1924 */

replace votes1_s=votesDNA if year>1920
replace votes1_nsa=votesNSA if year>1920
replace votes1_a=votesRF if year>1920
replace votes1_v=votesV if year>1920
replace votes1_l=votesSP if year>1920
replace votes1_h_fv=votesH_FV if year>1920
replace votes1_fv=votesFV if year>1920
replace votes1_nkp=votesNKP if year>1920
replace votes1_oth=votesOTH if year>1920
*** replace votes1_spr=votesVIL if year>1920  /* left out this category of "wild votes", see above */

keep year knr valgkrets rep votes1* votes2*
order year knr valgkrets rep votes1* votes2*
replace rep=1 if year<1920 /* SMD period */

egen votes1_TOTAL=rowtotal(votes1_s- votes1_nkp)
egen votes2_TOTAL=rowtotal(votes2_s- votes2_nkp)
order year knr valgkrets rep votes1* votes2*

sort year valgkrets knr 
************************************************************************
save dta/ImportVotesParty.dta, replace
************************************************************************
